Ext.define('appmod.controller.resources.Modules', {
    extend: 'Ext.app.Controller',

	models: [
		'resources.Module',
		'resources.ModuleFunction',
		'resources.MenuTreeModel',
		'resources.UrlInModuleModel',
		'resources.UiCompInModuleModel'
	],
	
    stores: [
		'resources.Modules',
		'resources.ChecksOfFunInModule',
		'resources.ModuleFunctions',
		'resources.MenuTreeInModule',
		'resources.UrlsInModule',
		'resources.UiCompUrlsInModule',
		'resources.UiCompInModule',
		'resources.UnloadedModules'
	],
    

    refs: [
		{ref: 'modulesList',selector:'moduledashboard gridpanel[region=center]'},
		{ref: 'moduleFunsList',selector:'moduledashboard gridpanel[region=east]'},
		{ref: 'disabledColumn',selector:'gridpanel[region=east] gridcolumn[dataIndex=disabled]'},
		{ref: 'funDetailViewWin',selector:'window[labelMark=funDetailView]'},
		{ref: 'functionDetailTabInView',selector:'functiondetailview tabpanel'},
		{ref: 'menuAuthView',selector:'functiondetailview menuauthview'},
		{ref: 'urlAuthView',selector:'functiondetailview urlauthview'},
		{ref: 'uicompAuthUrlView',selector:'functiondetailview uicompauthview gridpanel[region=west]'},
		{ref: 'uicompAuthView',selector:'functiondetailview uicompauthview gridpanel[region=center]'},
		{ref: 'functionDetailViewForm',selector:'functiondetailview'},
		{ref: 'moduleActCol',selector:'gridpanel[region=center] actioncolumn '},
		{ref: 'popupWin',selector:'window{isVisible()}'},
		{ref: 'unloadedModuleList',selector:'unloadmodulelist'}
	],

    views: [
    	'resources.module.Dashboard',
		'resources.module.UnloadModuleList',
		'resources.function.DetailView'
    ],
	

    init: function() {
		
		var me = this;

    	this.control({
			
			'moduledashboard gridpanel[region=center]': {
			    itemclick:this.clickAndSelectModule
			},
			'moduledashboard gridpanel[region=center] button[action=loadModule]': {
			    click:this.loadModule
			},
			'moduledashboard gridpanel[region=east] button[action=updateFunBootup]': {
			    click:this.updateFunBootup
			},	
			'moduledashboard gridpanel[region=east] button[action=viewFunDetail]': {
			    click:this.viewFunDetail
			},
			'window button[action=closeFunctionDetailView]': {
			    click:this.closeFunctionDetailView
			},
			'window button[action=closeWin]': {
			    click:this.closeWin
			},			
			'functiondetailview tabpanel': {
			    tabchange:this.changeTab
			},
			'functiondetailview uicompauthview gridpanel[region=west]' : {
				select : this.selectUiCompUrl
			}
								
    	});

    },
	
	loadInitComp : function() {
		var mainPanel = Ext.create('appmod.view.resources.module.Dashboard' , {
			region : 'center',
			border:false,
			padding:'0 0 0 0'
		});
		return mainPanel;		
	},
	

	/**
	 * defined and binding store --
	 */
	onLaunch:function() {
		
		var modulesList = this.getModulesList();
		
		modulesList.addEvents({
    		itemdelete: true
		});
		
		var dsOfModList = this.getResourcesModulesStore();
		modulesList.bindStore(dsOfModList);
		
		var modFunsList = this.getModuleFunsList();
		var dsOfFunList = this.getResourcesModuleFunctionsStore();
		modFunsList.bindStore(dsOfFunList);
		
		var dsOfCheckOfFunInModule = this.getResourcesChecksOfFunInModuleStore();
		var disabledComboCol = this.getDisabledColumn();
		var disabledCombo = disabledComboCol.getEditor();
		disabledCombo.bindStore(dsOfCheckOfFunInModule);
		
		
		var moduleActCol = this.getModuleActCol();

	},
	
	

	
	
	closeWin:function() {
		var popupWin = this.getPopupWin();
		popupWin.close();
	},
	
	loadModule:function() {
		
		var win = null;
		// --- build new form ---
		if (!win) {
			win = new Ext.create('widget.window', {
				title : '可加载的应用模块',
				closable : true,
				modal:true,
				modal:true,
				width : 600,
				height : 480,
				layout: 'border',
				bodyStytle : 'padding : 5px',
				items:[{
					padding : 5,
					defaults: {
        				labelWidth:65
    				},
					xtype : 'unloadmodulelist',
					region:'center'
				}],
				dockedItems: [{
				    xtype: 'toolbar',
				    dock: 'bottom',
				    ui: 'footer',
				    items: [
						'->',
				        {
            				text: '加 载',
							action:'loadUnloadedModule'
        				},						
				        {
            				text: '关 闭',
							action:'closeWin'
        				}
				    ]
				}]	
			});
		}
		
		// ---- load unload module list ----
		var dsOfUnMod = this.getResourcesUnloadedModulesStore();
		var unloadedModuleList = this.getUnloadedModuleList();		
		unloadedModuleList.bindStore( dsOfUnMod );

		if ( !win.isVisible() ) {
			win.show();
		}	

	},
	
	
	selectUiCompUrl:function(rowModel, record, index, eOpts) {
		var recId = record.getId();
		
		// --- get another panel store ---
		var uicompAuthView = this.getUicompAuthView();
		var store = uicompAuthView.getStore();
		
		var proxy = store.getProxy();

		proxy.extraParams = {
			urlId : recId
		};

		store.load();
	},
	
	changeTab:function(tabPanel, newCard, oldCard, eOpts) {
		var me = this;

		// --- check select item --
		var defListView = me.getModuleFunsList();	
		var selModel = defListView.getSelectionModel();
		var rec = selModel.getSelection();

		var currentXtype = newCard.getXType();


		// --- binding and load store
		var store = null;
		if ( currentXtype == 'urlauthview' || currentXtype == 'menuauthview') {
			store = newCard.getStore();
		}
		else if (currentXtype == 'uicompauthview') {
			var urlSelList = newCard.down('gridpanel[region=west]');
			store = urlSelList.getStore();
		}
		var funProxy = store.getProxy();
		funProxy.extraParams = {
			funId:rec[0].getId()
		};
		store.load();	


	},
	
	closeFunctionDetailView:function() {
		var funDetailViewWin = this.getFunDetailViewWin();
		if (funDetailViewWin.isVisible()) {
			funDetailViewWin.close();
		}

	},
	
	
	viewFunDetail:function() {
		
		me = this;
				
		// --- check select item --
		var defListView = me.getModuleFunsList();	
		var selModel = defListView.getSelectionModel();
		var rec = selModel.getSelection();

		if (rec.length == 0) {
			Ext.MessageBox.show({
        		title: "提示",
        		msg: '请先选择一项功能',
        		buttons: Ext.MessageBox.OK,
        		icon: Ext.MessageBox.WARNING
       		});
			return ;
		} else if (rec.length  > 1) {
			Ext.MessageBox.show({
        		title: "提示",
        		msg: '只能选择一项功能',
        		buttons: Ext.MessageBox.OK,
        		icon: Ext.MessageBox.WARNING
       		});
			return ;
		}		
		

		var win = null;
		// --- build new form ---
		if (!win) {
			win = new Ext.create('widget.window', {
				title : '模块功能关联状态详细视图',
				labelMark:'funDetailView',
				closable : true,
				modal:true,
				width : 600,
				height : 480,
				layout: 'border',
				bodyStytle : 'padding : 5px',
				items:[{
					padding : 5,
					defaults: {
        				labelWidth:65
    				},
					xtype : 'functiondetailview',
					region:'center'
				}],
				dockedItems: [{
				    xtype: 'toolbar',
				    dock: 'bottom',
				    ui: 'footer',
				    items: [
						'->',
				        {
            				text: '关 闭',
							action:'closeFunctionDetailView'
        				}
				    ]
				}]	
			});
		}
		
		
		// --- init tab panel in form ---
		var funDetailTab = me.getFunctionDetailTabInView();
		var activeTabPanel = funDetailTab.getActiveTab();
		
		// --- binding store ----
		var menuAuthView = me.getMenuAuthView();
		var urlAuthView = me.getUrlAuthView();
		var uicompAuthUrlView = me.getUicompAuthUrlView();
		var uicompAuthView = me.getUicompAuthView();
		
		
		var dsOfUrlInMod = this.getResourcesUrlsInModuleStore();
		urlAuthView.bindStore( dsOfUrlInMod );
		
		var dsOfUcUrlsInMod =  this.getResourcesUiCompUrlsInModuleStore();
		uicompAuthUrlView.bindStore( dsOfUcUrlsInMod );
		
		var dsOfUiCompInMod = this.getResourcesUiCompInModuleStore();
		uicompAuthView.bindStore( dsOfUiCompInMod );
		
	
		var dsOfBindingStore = activeTabPanel.getStore();
		var funProxy = dsOfBindingStore.getProxy();
		funProxy.extraParams = {
			funId:rec[0].getId()
		};

		if ( menuAuthView == activeTabPanel ) {
			dsOfBindingStore.setRootNode({
				expanded:true
			});
		} else {
			dsOfBindingStore.load();			
		}







		// --- init form data ---
		var basicForm = me.getFunctionDetailViewForm().getForm();
		if ( rec.length == 1 ) {
			basicForm.setValues({
				functionName:rec[0].get('name'),
				id:rec[0].getId()
			});
		}		
		
		
		
		if ( !win.isVisible() ) {
			win.show();
		}		
	},
	
	updateFunBootup:function() {
		
		var store = this.getResourcesModuleFunctionsStore();
		var proxy =store.getProxy();
		store.sync({
			callback:function(batch , options) {
				//alert('callback');
			},
			success:function(batch , options) {
				if ( batch.isComplete) {
					Ext.MessageBox.show({
        				title: "提示",
        				msg: '功能设定更新成功',
        				buttons: Ext.MessageBox.OK,
        				icon: Ext.MessageBox.WARNING
       				});
				}
			},
			failure:function(batch , options) {
				//alert('failure');
			}
		});


	},

	
	clickAndSelectModule:function (me, record, item, ndex, eveObj ) {
		var recName = record.get('name');
		var recId = record.getId();
		
		// --- get another panel store ---
		var functionPanel = this.getModuleFunsList();
		var funPanelStore = functionPanel.getStore();
		
		var proxy = funPanelStore.getProxy();

		proxy.extraParams = {
			modResId : recId
		};

		funPanelStore.load();


	}
	

	
});